package com.example.flinkdemo;


import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

public class FlinkKafka {

    public static void main(String[] args) {
        try {
            // 获取上下文环境StreamExecutionEnvironment对象
            final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.enableCheckpointing(10); // 要设置启动检查点
            env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);//设置事件触发时写入流
            // 配置kafka的ip和端口，以及消费者组
            Properties properties = new Properties();
            properties.setProperty("bootstrap.servers", "192.168.1.245:9092");
            properties.setProperty("group.id", "flume-kafka");
            //将消费者数据对象加入到上下文环境StreamExecutionEnvironment对象中，并生成DataStream对象；
            DataStreamSink<String> dataStream =env.
                    addSource(new FlinkKafkaConsumer<>("log4j-flume-kafka", new SimpleStringSchema(), properties))
                    .print();
            //设置job名称
            env.execute("consumer from kafka data");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}